#include "XpensRecord.h"

XpensRecord::XpensRecord()
{
	xpensTracing = new Tracing;
	ExpCategory.push_back("");
	ExpCategory.push_back("FOOD");
	ExpCategory.push_back("CLOTHES"); 
	ExpCategory.push_back("TRANSPORTATION");
	ExpCategory.push_back("ACCOMODATION");
	ExpCategory.push_back("ENTERTAINMENT");
	ExpCategory.push_back("EDUCATION");
	ExpCategory.push_back("INVESTMENT");
	ExpCategory.push_back("TELECOMMUNICATION");
	otherExp = 9;
	PayCategory.push_back("");
	PayCategory.push_back("");
	otherPay = 9;
}

Tracing* XpensRecord::getTracing()
{
	return xpensTracing;
}

bool XpensRecord::createItem(Item& newItem)
{
	if (newItem. record()){
		if (newItem.isexpense()) {
			if (newItem.getcat() > otherExp || newItem.getcat() < 1) {cout << "Invalid category input.\n" ; return false;}
			if (newItem.getcat() == otherExp){
				cout << "What Category?" << endl; 
				string newcat;
				cin >> newcat;
				ExpCategory.push_back(newcat);
				otherExp ++;}
			newItem.setCategory(ExpCategory[newItem.getcat()]);
			newItem. writefile();
			xpensTracing -> addTracing(newItem);}

		else {
			if (newItem.getcat() > otherPay || newItem.getcat() < 1) {cout << "Invalid category input.\n"; return false; }
			if (newItem.getcat() == otherPay){
				cout << "What Category?" << endl; 
				string newcat;
				cin >> newcat;
				PayCategory.push_back(newcat);
				otherPay ++;}
			newItem.setCategory(ExpCategory[newItem.getcat()]);
			newItem. writefile();
			xpensTracing -> addTracing(newItem);}

		return true;
	}
	return false;
};

void XpensRecord::deleteExpCat(int deleteID)
{
	vector <string>::iterator myIter;
	myIter = PayCategory.begin() + deleteID - 1;
	PayCategory.erase(myIter);
}

void XpensRecord::deletePayCay(int deleteID)
{
	vector <string>::iterator myIter;
	myIter = ExpCategory.begin() + deleteID - 1;
	ExpCategory.erase(myIter);
}
bool XpensRecord::updateItem( streamoff address, Item& newItem )
{	
	if (!newItem.checkEmpty()) xpensTracing -> addTracing( newItem );
//	xpensTracing.deleteTracing( originItem );

//	streamoff address = mySearch.internalSearch(originItem);

	char* filename;
	if (newItem.isexpense()) {
		filename = "Expense.txt";}
	else {
		filename = "Payment.txt";}
	
	ifstream infile(filename);
	string buffer;
	ofstream newStorage("temp.txt");
	
	infile.seekg(ios::beg);

	while ( address > infile.tellg() ){
		infile >> buffer;
		newStorage << buffer << ' ';
	}
	if (newItem.isexpense()) {
		XpensItem tempItem;
		tempItem.record_from_file(infile, infile.tellg());
		}
	else {
		PaymentItem tempItem;
		tempItem.record_from_file(infile, infile.tellg());}

	if (!newItem.checkEmpty()) {
		newItem.writefile();
	}
	
	while ( infile >> buffer ){
		newStorage << buffer << ' ';
	}
	newStorage.flush();
	infile.close();
	newStorage.close();

	ifstream newinfile ("temp.txt");
	ofstream outfile(filename);
	infile.seekg(ios::beg);
	
	while( newinfile >> buffer ){
		outfile << buffer << ' ';
	}
	outfile.flush();
	newinfile.close();
	outfile.close();
	return true;
}

void XpensRecord::setCategory()
{
	ifstream infile("Figure.txt");
	string category;
	infile >> category;
	while (category != "_")
	{
		ExpCategory.push_back(category);
		infile >> category;
	}

	while (infile >> category)
	{
		PayCategory.push_back(category);
		infile >> category;
	}
}

void XpensRecord::saveExit()
{
	ofstream outfile("Figure.txt",ios::app);
	int i;
	for (i = 1;i <= ExpCategory.size(); i ++)
		outfile << ExpCategory[i] << " ";
	outfile << "_ ";
	for (i = 1;i <= PayCategory.size(); i ++)
		outfile << PayCategory[i] << " ";
}

void XpensRecord::showRecord()
{
	

};